package com.wyw.design.model.learning.behavioral.emptyobject;

import java.util.Optional;

/**
 * @Title
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * @Date 2023/4/19 15:33
 * Update History:
 * Author        Time            Content
 */
public class Client2 {
    public static void main(final String[] args) {
        // JDK 8 Optional 对象判空示例
        // 具体对象
        User concreteUser = new User(new Address(new Country("China")));
        // 空对象
        User nullUser = new User(null);
        // 具体对象编码获取
        String concreteIsocode = Optional.ofNullable(concreteUser)
                .flatMap(u -> u.getAddress())
                .flatMap(a -> a.getCountry())
                .map(c -> c.getIsocode())
                .orElse("暂无").toUpperCase();
        // 空对象编码获取
        String nullIsocode = Optional.ofNullable(nullUser)
                .flatMap(u -> u.getAddress())
                .flatMap(a -> a.getCountry())
                .map(c -> c.getIsocode())
                .orElse("暂无").toUpperCase();
        System.out.println("Concrete User：" + concreteIsocode);
        System.out.println("Null User：" + nullIsocode);
    }

}
